package cn.wolfcode.web.controller;

import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageBoardService;
import cn.wolfcode.service.IMessageReplyService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/messageBoard")
public class MessageBoardController {
    @Autowired
    private IMessageBoardService messageBoardService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;
    @Autowired
    private IMessageReplyService messageReplyService;


    @RequestMapping("/list")
    @RequirePermission(name = "部门查询",expression = "messageBoard:list")
    public String list(Model model, QueryObject qo) {
        model.addAttribute("pageInfo", messageBoardService.query(qo));
        // 业务下拉框
        model.addAttribute("business", systemDictionaryService.queryBySn("business"));
        return "messageBoard/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "部门删除",expression = "messageBoard:delete")
    public String delete(Long id) {
        if (id != null) {
            messageBoardService.delete(id);
        }
        return "redirect:/messageBoard/list";
    }

    // 查看
    @RequestMapping("/detail")
    public String detail(Long id,Model model) {
        if (id != null) {
            model.addAttribute("m", messageBoardService.get(id));
            model.addAttribute("messages", messageReplyService.getReply(id));
        }
        return "messageBoard/detail";
    }

    /*@RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "部门编辑",expression = "messageBoard:saveOrUpdate")
    public String saveOrUpdate(MessageBoard messageBoard) {
        if (messageBoard.getId() != null) {
            messageBoardService.update(messageBoard);
        } else {
            messageBoardService.insert(messageBoard);
        }
        return "redirect:/messageBoard/list";
    }*/

    // 异步提交表单方式
    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "部门编辑", expression = "messageBoard:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(MessageBoard messageBoard) {
        if (messageBoard.getId() != null) {
            messageBoardService.update(messageBoard);
        } else {
            messageBoardService.insert(messageBoard);
        }
        return new JsonResult(true,"添加成功");
    }
}
